var c = require('../lib/common.js');
var cf = require('../lib/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wechatpay: '/images/dui.png',
    integral: '/images/select.png',
    status: 1 //支付方式 1微信 2积分
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    c.verlogin(this);
    var userinfo = wx.getStorageSync('userinfo');
    var order_id = options.order_id;
    this.setData({
      order_id: order_id,
      userinfo: userinfo
    });
    var that = this;
    c.ajax(
      cf.hostapi + 'toIdGetOrderInfo', {
        order_id: order_id
      },
      res => {
        console.log(res.data.data);
        that.setData({
          data: res.data.data
        })
      }
    )
  },
  clickpay: function(e) {
    var status = e.currentTarget.dataset.status;
    if (status == 1) {
      this.setData({
        wechatpay: '/images/dui.png',
        integral: '/images/select.png',
        status: status
      })
    } else {
      this.setData({
        wechatpay: '/images/select.png',
        integral: '/images/dui.png',
        status: status
      })
    }
  },

  userinfo: function(e) {
    c.clicklogin(e, this);
  },
  submit: function() {
    var status = this.data.status;
    var order_id = this.data.order_id;
    var orderdata=this.data.data;
    // 微信支付
    if (status == 1) {
      var openid = wx.getStorageSync('userinfo').openid;
      wx.request({
        url: cf.hostapi + 'wxpay',
        data: {
          openid: openid,
          order_id: order_id
        },
        success: function(response) {
          console.log('response');
          console.log(response);

          var miyao = response.data.miyao;
          var lghtime = response.data.lghtime
          var appId = response.data.appid;
          var timeStamp = (Date.parse(new Date()) / 1000).toString();
          var pkg = 'prepay_id=' + response.data.prepay_id;
          var payid = response.data.prepay_id;
          var nonceStr = response.data.nonce_str;
          var md5 = require('../../utils/md5.js');
          var paySign = md5.hex_md5('appId=' + appId + '&nonceStr=' + nonceStr + '&package=' + pkg + '&signType=MD5&timeStamp=' + timeStamp + "&key=" + miyao).toUpperCase();
          wx.requestPayment({
            'timeStamp': timeStamp,
            'nonceStr': nonceStr,
            'package': pkg,
            'signType': 'MD5',
            'paySign': paySign,
            'success': function(res) {
              console.log(res);
              if (res.errMsg == "requestPayment:ok") {
                wx.switchTab({
                  url: '/pages/index/index',
                })

              } else {
                //tode支付失败之后逻辑问题
              }
            },
            'fail': function(res) {
              wx.showToast({
                title: '支付失败',
                image: '/images/closered.png',
              })
              return;
            }
          });
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
      });
    } else {
      if (orderdata.add_money > 0 || is_insureds==1) {
        wx.showToast({
          title: '此订单不可以积分支付',
          image: '/images/closered.png'
        })
        return;
      }
      //积分支付
      wx.showLoading({
        title: '支付中...',
      })
      var uid = wx.getStorageSync('userinfo').id;
      c.ajax(
        cf.hostapi + 'integralPay', {
          order_id: order_id,
          uid: uid
        },
        res => {
          if (res.data.retcode == 200) {
            wx.hideLoading();
            wx.showToast({
              title: '支付成功！',
              image: '/images/dui.png'
            })
            setTimeout(function() {
              wx.redirectTo({
                url: '/pages/my-order/my-order',
              })
            }, 1000)
          } else if (res.data.retcode == 401) {
            wx.hideLoading();
            wx.showToast({
              title: '积分不足！',
              image: '/images/closered.png'
            })
          };

        }
      )
    }








  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})